package com.sunshine.scw.base.dao;

import java.io.Serializable;
import java.util.List;

/**
 * 分页工具类
 * 
 * @author xiao
 * @version 1.0
 */
public class Pagination<T> implements Serializable {
	private static final long serialVersionUID = -1616146597405643391L;

	/* 当前页 */
	private int currentPage = 1;
	/* 每页显示的记录数 */
	private int pageSize = 3;
	/* 总记录数 */
	private int totalCount = 0;
	/* 总页数 */
	private int pageCount = 0;

	/* 是否为第一页 */
	private boolean firstPage = false;
	/* 是否为最后一页 */
	private boolean lastPage = false;
	/* 是否有上一页 */
	private boolean pageUp = false;
	/* 是否有下一页 */
	private boolean pageDown = false;
	/* 导航页码数 */
	private int navigatePages = 6;
	/* 所有导航页号 */
	private int[] navigatePageNumbers;

	/* 对象记录结果集 */
	private List<T> recordList;

	/**
	 * 无参构造函数
	 */
	public Pagination() {
	}

	/**
	 * 构造函数
	 * 
	 * @param currentPage
	 *            当前页
	 */
	public Pagination(int currentPage) {
		this.currentPage = currentPage;
	}

	/**
	 * 构造函数
	 * 
	 * @param currentPage
	 *            当前页
	 * @param pageSize
	 *            每页显示的记录数
	 */
	public Pagination(int currentPage, int pageSize) {
		this.currentPage = currentPage;
		this.pageSize = pageSize;
	}

	/**
	 * 初始化方法
	 * 
	 * @param currentPage
	 * @param pageSize
	 * @param totalCount
	 */
	public void init() {
		int currentPage = this.currentPage;
		int pageSize = this.pageSize;
		int totalCount = this.totalCount;
		if (totalCount == 0)
			return;
		this.pageCount = totalCount % pageSize == 0 ? totalCount / pageSize
				: totalCount / pageSize + 1;
		if (currentPage < 1)
			currentPage = 1;
		else if (pageSize > pageCount)
			pageSize = pageCount;
		else
			this.pageSize = pageSize;
		calcNavigatePageNumbers();
		judgePageBoudary();
	}

	private void calcNavigatePageNumbers() {
		// 当总页数小于或等于导航页码数时
		if (pageCount <= navigatePages) {
			navigatePageNumbers = new int[pageCount];
			for (int i = 0; i < pageCount; i++) {
				navigatePageNumbers[i] = i + 1;
			}
		} else {// 当总页数大于导航页码数时
			navigatePageNumbers = new int[navigatePages];
			int startNum = currentPage - navigatePages / 2;
			int endNum = currentPage + navigatePages / 2;
			if (startNum < 1) {
				startNum = 1;
				// 最前navigatePages页
				for (int i = 0; i < navigatePages; i++) {
					navigatePageNumbers[i] = startNum++;
				}
			} else if (endNum > pageCount) {
				endNum = this.pageCount;
				// 最后navigatePages页
				for (int i = navigatePages - 1; i >= 0; i--) {
					navigatePageNumbers[i] = endNum--;
				}
			} else {
				// 所有中间页
				for (int i = 0; i < navigatePages; i++) {
					navigatePageNumbers[i] = startNum++;
				}
			}
		}
	}

	/**
	 * 判定页面页码边界
	 */
	private void judgePageBoudary() {
		firstPage = currentPage == 1;
		lastPage = currentPage == pageCount;
		pageUp = currentPage > 1;
		pageDown = currentPage < pageCount;
	}

	public int getFirstResultCount() {
		return (currentPage - 1) * pageSize;
	}

	/**
	 * 得到当前页码
	 * 
	 * @return
	 */
	public int getCurrentPage() {
		return currentPage;
	}

	/**
	 * 设置当前页码
	 * 
	 * @param currentPage
	 */
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	/**
	 * 得到每页显示的记录数
	 * 
	 * @return
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * 设置每页显示的记录数
	 * 
	 * @param pageSize
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * 得到总记录数
	 * 
	 * @return
	 */
	public int getTotalCount() {
		return totalCount;
	}

	/**
	 * 设置总记录数
	 * 
	 * @param totalCount
	 */
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	/**
	 * 得到总页数
	 * 
	 * @return
	 */
	public int getPageCount() {
		return pageCount;
	}

	/**
	 * 得到每页显示的结果集
	 * 
	 * @return
	 */
	public List<T> getRecordList() {
		return recordList;
	}

	public void setRecordList(List<T> recordList) {
		this.recordList = recordList;
	}

	/**
	 * 得到导航页码数
	 * 
	 * @return
	 */
	public int getNavigatePages() {
		return navigatePages;
	}

	/**
	 * 设置导航页码数
	 * 
	 * @param navigatePages
	 */
	public void setNavigatePages(int navigatePages) {
		this.navigatePages = navigatePages;
	}

	/**
	 * 得到所有导航页号
	 * 
	 * @return
	 */
	public int[] getNavigatePageNumbers() {
		return navigatePageNumbers;
	}

	public boolean isFirstPage() {
		return firstPage;
	}

	public boolean isLastPage() {
		return lastPage;
	}

	public boolean isPageUp() {
		return pageUp;
	}

	public boolean isPageDown() {
		return pageDown;
	}

}
